﻿using BPMN.Application.IService;
using BPMN.Domain;
using BPMN.Domain.Entity.User;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BPMN.DomianShare;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using BPMN.Application.Contracts.DTO.Request.UserInfo;
using BPMN.Application.Contracts.DTO.Request.Role;
using BPMN.Application.Contracts.DTO.Response.Permission;
using BPMN.Application.Contracts.DTO.Response;
using System.Security.Cryptography;
using BPMN.Application.Contracts.DTO.Request.Menu;
using Microsoft.VisualBasic;
using BPMN.Application.Contracts.DTO.Request.Menu.Permission;

namespace BPMN.Application.Service
{
    public class PermissionAppService : AppServiceBase<Permission>, IPermissionAppService
    {
        private readonly IRepositoryBase<Permission> RepPermission;
        private readonly IRepositoryBase<Menu> reMenu;
        private readonly IMapper mapper;


        public PermissionAppService(IRepositoryBase<Permission> RepPermission, IRepositoryBase<Menu> reMenu, IMapper mapper) : base(RepPermission)
        {
            this.RepPermission = RepPermission;
            this.reMenu = reMenu;
            this.mapper = mapper;
        }
        /// <summary>
        ///  权限添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddPermission(PermissionRequstDto entity)
        {
            var slist = RepPermission.GetAll().Result.Where(x => x.Name == entity.Name && x.IsDel == true).FirstOrDefault();
            if (slist != null)
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "该权限已存在");
            }
            var obj = mapper.Map<Permission>(entity);
            int n = await RepPermission.Add(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }
        /// <summary>
        /// 获取最高级菜单
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<PermissionShowResponseDto>>> GetPermissionMax()
        {
            return new ResultResponseDto<List<PermissionShowResponseDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<PermissionShowResponseDto>>(RepPermission.GetAll().Result.Where(x => x.IsDel == true && x.Pid == 0).ToList())
            };
        }
        /// <summary>
        /// 权限显示功能
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<TreeDtoResponse>>> GetPermission()
        {
            var slist = RepPermission.GetAll().Result.Where(x => x.IsDel == true).ToList();

            return new ResultResponseDto<List<TreeDtoResponse>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = GetRerTree(slist, 0)
            };
        }
        /// <summary>
        /// 树形结构
        /// </summary>
        /// <param name="list"></param>
        /// <param name="Pid"></param>
        /// <returns></returns>
        private List<TreeDtoResponse> GetRerTree(List<Permission> list, int Pid)
        {
            var slist = list.Where(x => x.Pid == Pid).ToList();
            var result = from A in slist
                         select new TreeDtoResponse
                         {
                             value = A.Id,
                             label = A.Name,
                             URL = A.URL,
                             ButtonValue = A.ButtonJson.Split(','),
                             children = GetRerTree(list, A.Id),
                             strings=new string[0],
                             pid=A.Pid,
                         };
            return result.Count() == 0 ? null : result.ToList();
        }
        /// <summary>
        /// 根据Id获取权限信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<PermissionShowResponseDto>> GetPermissionById(int Id)
        {
            var slist = await RepPermission.Get(Id);

            return new ResultResponseDto<PermissionShowResponseDto>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<PermissionShowResponseDto>(slist)
          
            
            };

        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdatePermission(PermissionRequstDto entity)
        {
            var slist = await (await RepPermission.GetAll()).FirstOrDefaultAsync();

            var obj = mapper.Map<Permission>(entity);
            int n = await RepPermission.Update(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>(); 
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }
        /// <summary>
        /// 权限删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> DelPer(int Id)
        {
            var slist = RepPermission.GetAll().Result.FirstOrDefault(x => x.Pid == Id);
            if (slist != null)
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "该权限已有子集不能删除");
            }

            int n = await RepPermission.Delete(Id);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "删除失败");
            }

        }



        
    }
}

